package org.zmeng.demo;

public class Test {

	public static void main(String[] args) {
		int[] arr = {1, 3, 5, 6, 7, 8};
		System.out.println(binarySearch(arr, 0, arr.length, 8));
	}
	
	
	private static int binarySearch(int[] arr, int fromIndex, int endIndex, int key) {
		int low = fromIndex;
		int high = endIndex;
		
		while(low <= high) {
			int mid = (low + high) >>> 1;
			int midVal = arr[mid];
			
			if (midVal > key) {
				low = mid + 1;
			} else if (midVal < key) {
				high = mid - 1;
			} else {
				return mid;
			}
		}
		return -1;
	}
}
